package cn.aresoft.web.action;

import org.apache.commons.lang.StringUtils;

import cn.aresoft.framework.support.action.BaseActionSupport;
import cn.aresoft.logic.service.face.custom.IBasicUserInfoService;
import cn.aresoft.model.BasicUserInfo;
import cn.aresoft.web.WebConstants;

public class LoginAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;
	
	/**
	 * 用户登录操作
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception{
		
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		userInfo = new BasicUserInfo();
		userInfo.setUsername(userName);
		userInfo = (BasicUserInfo) iBasicUserInfoService.load("load", userInfo);
		
		if(userInfo==null){
			this.getRequest().setAttribute("msg", "登录失败:用户名不存在！"); 
			return ActionResult.INPUT;
		}else if(!password.equals(userInfo.getPassword())){
			this.getRequest().setAttribute("msg", "登录失败:用户名密码不匹配！"); 
			return ActionResult.INPUT;
		}else if(userInfo.getStatus()==1){
			this.getRequest().setAttribute("msg", "登录失败:账户未激活，请与管理员联系"); 
			return ActionResult.INPUT;
		}else{
			//缓存用户信息
			session.put(WebConstants.USER_SESSION_KEY, userInfo); 

			//执行上一步操作
			String goingToURL = (String) session.get(WebConstants.GOING_TO_URL_KEY);   
			if (StringUtils.isNotBlank(goingToURL)){   
				session.remove(WebConstants.GOING_TO_URL_KEY);   
			}else{   
			    setGoingToURL("index.action");   
			}
			return ActionResult.SUCCESS;
		}
	}
	
	/**
	 * 注销操作
	 * @return
	 * @throws Exception
	 */
	public String logout() throws Exception{   
        if (session!=null){
            session.remove(WebConstants.USER_SESSION_KEY);   
        	session.remove(WebConstants.GOING_TO_URL_KEY);
        }
        return "login";   
    }  
	
	
	
	private IBasicUserInfoService iBasicUserInfoService;
	private BasicUserInfo userInfo;
	private String goingToURL; 

	public void setGoingToURL(String goingToURL) {
		this.goingToURL = goingToURL;
	}

	public BasicUserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(BasicUserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public void setIBasicUserInfoService(IBasicUserInfoService basicUserInfoService) {
		iBasicUserInfoService = basicUserInfoService;
	}
}	